package com.ggh.jinjilive.ui.mine.ranking;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentRankingBangBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.mine.ranking.RankingBangModel;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mine.ranking.adapter.RankingBangAdapter;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RankingBangFragment extends BaseFragment<FragmentRankingBangBinding> implements OnRefreshListener, OnLoadMoreListener,View.OnClickListener, AddItemOnClickListener {
    //适配器
    private RankingBangAdapter rankingBangAdapter;
    private int page = 1;
    //查询时间(日榜:’today’,周榜:’week’,月榜:’month’,总榜：’’)
    private String time = "today";
    //位置
    private int position;
    public RankingBangFragment(int position){
        this.position = position;

    }


    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_ranking_bang;
    }

    @Override
    public void initView() {
        super.initView();
        loadService = LoadSir.getDefault().register(binding.refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //获取订单信息
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
    }

    @Override
    public void initData() {
        super.initData();
        rankingBangAdapter = new RankingBangAdapter();
        rankingBangAdapter.setAddItemOnClickListener(this);
        binding.rvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvGoods.setAdapter(rankingBangAdapter);
        //获取排行榜数据
        onRefresh(binding.refreshLayout);
        //日榜
        binding.tvDayBang.setBackgroundResource(R.drawable.bg_50_0080ff);
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //日榜
        binding.tvDayBang.setOnClickListener(this);
        //周榜
        binding.tvWeeksBang.setOnClickListener(this);
        //月榜
        binding.tvMonthBang.setOnClickListener(this);
        //总榜
        binding.tvOverallBang.setOnClickListener(this);
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    public void onClick(View view) {
        //日榜
        binding.tvDayBang.setBackgroundResource(R.drawable.bg_1_50_f);
        //周榜
        binding.tvWeeksBang.setBackgroundResource(R.drawable.bg_1_50_f);
        //月榜
        binding.tvMonthBang.setBackgroundResource(R.drawable.bg_1_50_f);
        //总榜
        binding.tvOverallBang.setBackgroundResource(R.drawable.bg_1_50_f);
        //
        switch (view.getId()){
            case R.id.tv_dayBang://日榜
                //日榜
                time = "today";
                binding.tvDayBang.setBackgroundResource(R.drawable.bg_50_0080ff);
                break;
            case R.id.tv_weeksBang://周榜
                //周榜
                time = "week";
                binding.tvWeeksBang.setBackgroundResource(R.drawable.bg_50_0080ff);
                break;
            case R.id.tv_monthBang://月榜
                //月榜
                time = "month";
                binding.tvMonthBang.setBackgroundResource(R.drawable.bg_50_0080ff);
                break;
            case R.id.tv_overallBang://总榜
                //总榜
                time = "";
                binding.tvOverallBang.setBackgroundResource(R.drawable.bg_50_0080ff);
                break;
        }
        //获取排行榜数据
        onRefresh(binding.refreshLayout);
    }


    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        Map<String, Object> params = new HashMap<>();
        if(position == 0){
            params.put("type",1);
        }else if(position == 1){
            params.put("type",2);
        }
        //(日榜:’today’,周榜:’week’,月榜:’month’,总榜：’’)
        params.put("time", time);
        page = page + 1;
        //分页
        params.put("page", page);
        GeneralUtils.infoRankingList(params, new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                List<RankingBangModel> rankingBangModelList = (List<RankingBangModel>) mObject;
                if(null != rankingBangModelList){
                    if(rankingBangModelList.size() > 0){
                        rankingBangAdapter.add(rankingBangModelList);
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        Map<String, Object> params = new HashMap<>();
        if(position == 0){
            params.put("type",1);
        }else if(position == 1){
            params.put("type",2);
        }
        //(日榜:’today’,周榜:’week’,月榜:’month’,总榜：’’)
        params.put("time", time);
        page = 1;
        //分页
        params.put("page", page);
        GeneralUtils.infoRankingList(params, new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                //完成刷新
                binding.refreshLayout.finishRefresh();
                List<RankingBangModel> rankingBangModelList = (List<RankingBangModel>) mObject;
                if(null != rankingBangModelList){
                    if(rankingBangModelList.size() > 0){
                        loadService.showSuccess();
                        rankingBangAdapter.setAdd(rankingBangModelList);
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }
            }
        });
    }

    @Override
    public void onSkipClicked(int position, Object mObject) {
        RankingBangModel rankingBangModel = (RankingBangModel) mObject;
        if(null != rankingBangModel){
            Map<String, Object> params = new HashMap<>();
            params.put("be_user_id",rankingBangModel.getUser_id());
            GeneralUtils.myFollow(params, new AddOnClickListener1() {
                @Override
                public void onSkipClicked(Object mObject) {
                    //刷新数据
                    onRefresh(binding.refreshLayout);
                }
            });
        }
    }
}
